package cn.kgc.itrip.service.commentService;

import cn.kgc.itrip.common.Constants;
import cn.kgc.itrip.common.DtoUtil;
import cn.kgc.itrip.common.EmptyUtils;
import cn.kgc.itrip.common.ValidationToken;
import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.mapper.comment.CommentMapper;
import cn.kgc.itrip.mapper.itripComment.ItripCommentMapper;
import cn.kgc.itrip.mapper.itripHotel.ItripHotelMapper;
import cn.kgc.itrip.pojo.*;
import cn.kgc.itrip.vo.ItripImageVO;
import cn.kgc.itrip.vo.comment.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import sun.awt.image.BufImgVolatileSurfaceManager;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: love_travel
 * @description: 实现类
 * @author: Mr.Wang
 * @create: 2020-04-05 15:08
 **/

@Service
public class CommentServiceImpl implements CommentService{

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ValidationToken validationToken;
    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripHotelMapper itripHotelMapper;



    /**
     * 查询出游类型列表
     * @return
     */
    @Override
    public Dto findTravelType() {
        List<ItripLabelDic> labelDicList=commentMapper.findTravelType();
        if (EmptyUtils.isNotEmpty(labelDicList)){
            return DtoUtil.returnDataSuccess(labelDicList);
        }
        return DtoUtil.returnFail(" 获取旅游类型列表错误","100019");
    }

    /**
     * 根据targetId查询评论照片(type=2)
     * @param targetId
     * @return
     */
    @Override
    public Dto findImg(String targetId) {
        if (EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail("评论id不能为空","100013");
        }
        int i= Integer.parseInt(targetId);
        List<ItripImage> itripImage =commentMapper.findTargetIdImg(i);
        if (EmptyUtils.isNotEmpty(itripImage)){
            ItripImageVO itripImageVO=null;
            List<ItripImageVO> imageVOS=new ArrayList<>();
            for (ItripImage image:itripImage){
                itripImageVO=new ItripImageVO();
                BeanUtils.copyProperties(image,itripImageVO);
                imageVOS.add(itripImageVO);
            }
            return DtoUtil.returnDataSuccess(imageVOS);
        }
        return DtoUtil.returnFail("获取评论图片失败","100012");
    }

    /**
     * 新增评论接口
     * @param vo
     * @param token
     * @param userAgent
     * @return
     */
    @Override
    public Dto addAppraise(ItripAddCommentVO vo, String token, String userAgent)throws Exception {
        if (!validationToken.validateToken(token,userAgent)){
            return DtoUtil.returnFail("token失效，请重登录","100000");
        }
        if (EmptyUtils.isEmpty(vo.getOrderId())){
            return DtoUtil.returnFail("新增评论，订单ID不能为空","100005");
        }
        if (vo.getFacilitiesScore()==null ||vo.getFacilitiesScore()==0||
        vo.getHotelId()==null||vo.getHotelId()==0||
                vo.getHygieneScore()==null||vo.getHygieneScore()==0||
                vo.getIsHavingImg()==null||vo.getIsHavingImg()==0||
                vo.getPositionScore()==null||vo.getPositionScore()==0||
                vo.getProductId()==null||vo.getProductId()==0||
                vo.getProductType()==null||vo.getProductType()==0||
                vo.getServiceScore()==null||vo.getServiceScore()==0||
                vo.getTripMode()==null){
            return DtoUtil.returnFail("不能提交空，请填写评论信息","100004");
        }
        ItripComment comment=new ItripComment();
        comment.setHotelId(vo.getHotelId());
        comment.setOrderId(vo.getOrderId());
        comment.setIsHavingImg(vo.getIsHavingImg());
        comment.setHygieneScore(vo.getHygieneScore());
        comment.setPositionScore(vo.getPositionScore());
        comment.setProductId(vo.getProductId());
        comment.setProductType(vo.getProductType());
        comment.setScore(vo.getServiceScore());
        comment.setFacilitiesScore(vo.getFacilitiesScore());
        comment.setTripMode(Long.valueOf(vo.getTripMode()));
        Integer i = itripCommentMapper.updateItripComment(comment);
        if (EmptyUtils.isNotEmpty(i)){
            return DtoUtil.returnSuccess();
        }
        return DtoUtil.returnFail("新增评论失败","100003");
    }

    /**
     * 根据评论类型查询评论列表，并分页显示
     * @param vo
     * @return
     */
    @Override
    public Dto findCommentList(ItripSearchCommentVO vo)throws Exception {
        if (EmptyUtils.isEmpty(vo.getHotelId())){
            return DtoUtil.returnFail("获取评论列表错误","100020");
        }
        if (EmptyUtils.isEmpty(vo.getIsHavingImg())){
            return DtoUtil.returnFail("获取评论列表错误","100020");
        }
        if (EmptyUtils.isEmpty(vo.getIsOk())){
            return DtoUtil.returnFail("获取评论列表错误","100020");
        }
        //设置当前页
        //设置当前页，默认从第一页开始
        int pageNo= Constants.DEFAULT_PAGE_NO;
        if (EmptyUtils.isNotEmpty(vo.getPageNo())){
            pageNo=vo.getPageNo();
        }
        //每页显示的数量
        //设置显示的数量
        int pageSize=Constants.DEFAULT_PAGE_SIZE;
        if (EmptyUtils.isNotEmpty(vo.getPageSize())){
            pageSize=vo.getPageSize();
        }
        Map<String, Object> param=new HashMap<>();
        param.put("beginPos",pageNo);
        param.put("pageSize",pageSize);
        param.put("hotelId",vo.getHotelId());
        param.put("isHavingImg",vo.getIsHavingImg());
        param.put("isOk",vo.getIsOk());
        List<ItripComment> listByMap = itripCommentMapper.getItripCommentListByMap(param);
        if (EmptyUtils.isNotEmpty(listByMap)){
            return DtoUtil.returnDataSuccess(listByMap);
        }
        return DtoUtil.returnFail("获取评论列表错误","100020");
    }

    /**
     * 获取酒店相关信息（酒店名称、酒店星级）
     * @param hotelId
     * @return
     */
    @Override
    public Dto findHotelDesc(String hotelId)throws Exception {
        if (EmptyUtils.isEmpty(hotelId)){
            return DtoUtil.returnFail("获取酒店相关信息错误","100021");
        }
        ItripHotel hotel = itripHotelMapper.getItripHotelById(Long.valueOf(hotelId));
        if (EmptyUtils.isNotEmpty(hotel)){
            ItripHotelDescVO vo=new ItripHotelDescVO();
            vo.setHotelId(hotel.getId());
            vo.setHotelName(hotel.getHotelName());
            vo.setHotelLevel(hotel.getHotelLevel());
            return DtoUtil.returnDataSuccess(vo);
        }
        return DtoUtil.returnFail("获取酒店相关信息错误","100021");
    }

    /**
     * 据酒店id查询酒店平均分
     * @param hotelId
     * @return
     */
    @Override
    public Dto findHotelScore(String hotelId)throws Exception {
        if (EmptyUtils.isEmpty(hotelId)){
            return DtoUtil.returnFail("hotelId不能为空","100002");
        }
        ItripScoreCommentVO vo = commentMapper.findHotelScore(Long.valueOf(hotelId));
        if (EmptyUtils.isNotEmpty(vo)){
            return DtoUtil.returnDataSuccess(vo);
        }
        return DtoUtil.returnFail("hotelId不能为空","100002");
    }

    /**
     * 根据酒店id查询各类评论数量
     * @param hotelId
     * @return
     */
    @Override
    public Dto findCount(String hotelId)throws Exception {
        if (EmptyUtils.isEmpty(hotelId)){
            return DtoUtil.returnFail("参数hotelId为空","100018");
        }
        Integer count = 0;
        //sql参数需要的map类型
        Map<String,Object> param = new HashMap<String,Object>();
        //返回int类型，没有dto，所以用map类型封装成json数据返回
        Map<String,Integer> countMap = new HashMap<String,Integer>();

        //获取总评论数量
        param.put("hotelId",hotelId);
        count = itripCommentMapper.getItripCommentCountByMap(param);
        if (EmptyUtils.isEmpty(count)){
            return DtoUtil.returnFail("获取酒店总评论数失败","100014");
        }
        countMap.put("allcomment",count);

        //获取有待改善评论数量
        param.put("isOk",0);//0：有待改善 1：值得推荐
        count = itripCommentMapper.getItripCommentCountByMap(param);
        if (EmptyUtils.isEmpty(count)){
            return DtoUtil.returnFail("获取酒店有待改善评论数失败","100016");
        }
        countMap.put("improve",count);

        //获取值得推荐评论数量
        param.put("isOk",1);//0：有待改善 1：值得推荐
        count = itripCommentMapper.getItripCommentCountByMap(param);
        if (EmptyUtils.isEmpty(count)){
            return DtoUtil.returnFail("获取酒店值得推荐评论数失败","100017");
        }
        countMap.put("isok",count);

        //获取图片评论数量
        param.put("isHavingImg",1);//0:无图片 1:有图片
        param.put("isOk",null);
        count = itripCommentMapper.getItripCommentCountByMap(param);
        if (EmptyUtils.isEmpty(count)){
            return DtoUtil.returnFail("获取酒店有图片评论数失败","100015");
        }
        countMap.put("havingimg",count);

        return DtoUtil.returnDataSuccess(countMap);
    }
}
